package views
{
	import assists.caches.SaveZipAsFile;
	import assists.resource.ResourceLoader;
	import assists.resource.ResourceLoaderData;
	
	import flash.utils.getTimer;
	
	import inerface.INavigationTypeChoose;
	
	import main.LocalStoreInfo;
	import main.ViewSubBase;
	
	import panel.viewSpots.navigationNew.NavigationTypeChoose;
	
	public class NavigationTypeChooseView extends ViewSubBase
	{
		private var _key:String = "6";
		private var _panel:INavigationTypeChoose;
		private var loadZip:SaveZipAsFile;
		private var store:LocalStoreInfo = new LocalStoreInfo();
		private var _hasZip:Boolean;
		
		public function NavigationTypeChooseView()
		{
			super();
		}
		
		public override function show():void
		{
			
			var callBack:Function = function():void
			{
				resourceLoader.load([
					new ResourceLoaderData(data.uri.getNavigationPic("map",_key))
				],new Function,render);
			}
			this.data.xmlLoaderManager.navigationNew.init(this.data.uri.getXML(_key),callBack);
		}
		
		public function showData(info:Object):void
		{
			view.loadWait.show();
			_key = info.id;
			loadZipFunc();
		}
		
		protected override function render():void
		{
			loadSuccess();
			view.loadWait.close();
			if(!_panel)
			{
				_panel = new NavigationTypeChoose();
				_panel.init(this.layout.stage,this.resourceLoader,this.lang,this.data.uri);
				_panel.onFunc(openSoundView,view.navigationDetail.showComp,view.weico.show);
		   	_panel.show(this.data.xmlLoaderManager.navigationNew.getData(this.data.uri.getNavigationXml(_key)),resourceLoader.getImage(data.uri.getNavigationPic("map",_key)));
			}
			this.view.guide.upAndDownVisible(false,false);
			this.addView(_panel);
		}
		
		private function loadSuccess():void
		{
			var obj:Object = store.readInfo("downloadInfo");
			if(obj != null && _hasZip){
				obj.downloadList.push(_key);
			}else{
				obj = new Object;
				obj = {downloadList:[_key]};
				store.storeInfo("downloadInfo",obj);
			}
			
		}
		
		private function loadZipFunc():void
		{
			var obj:Object = store.readInfo("downloadInfo");
			if(obj != null)
			{
				for(var i:int = 0;i  < obj.downloadList.length;i++){
					if(obj.downloadList[i] == _key){
						render();
						return;
					}
					}
				_hasZip = true;
			}
			
			loadZip = new SaveZipAsFile();
			
			var progressFunc:Function = function(key:String,current:int,total:int):void
			{
				view.loadWait.showProccess(current,total);
			}
			var callBack:Function = function():void
			{
				resourceLoader.load([
					new ResourceLoaderData(data.uri.getNavigationPic("map",_key))
				],progressFunc,loadXml);
				
			}
				
			var url:String = data.uri.getScenicZip("shandong-weihai/scenic/shandong-weihai-" + _key);
			loadZip.unzipSuccess = callBack;
			loadZip.unzip_init(url,2,"/shandong-weihai/scenic/weihai");
		}
		
		private function loadXml():void
		{
			this.data.xmlLoaderManager.navigationNew.init(this.data.uri.getNavigationXml(_key),render)
		}
		
		public function get key():String
		{
			return _key;
		}
		
		private function openSoundView(soundUrl:String,desc:String = ""):void
		{
			view.media.soundName = desc;
			view.media.soundUrl = data.uri.getNavigationMp3(soundUrl,_key);
			view.media.onDelayFunc = function ():void
			{
			_panel.onSoundMedia = view.media.soundMedia;
			}
			view.media.show();
		}
		
		override public function clear():void
		{
			this.view.guide.upAndDownVisible(true,true);
			_hasZip = false;
		}
		
	}
}